import re
import oss2
from oss2.models import BucketReferer
import time
# 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth('LTAI4FreXSrQheqtqsKeR2iR', 'hjGZJVv6vnN2BwkQBGzE6n4oq8hqrZ')
# Endpoint以杭州为例，其它Region请按实际情况填写。
bucket = oss2.Bucket(auth, 'oss-cn-qingdao-internal.aliyuncs.com', 'fzj-202001')
bucket.put_bucket_referer(BucketReferer(True, []))
now_time=time.localtime()
now_year=now_time.tm_year
now_mon=now_time.tm_mon
prefix_list=['zimu/','ibf/','csdn/','bakmysql/','bakpostsql/','nginx_log/','redis_logs/','yuwuzheng/']
for prefix in prefix_list:
    for b in oss2.ObjectIterator(bucket, prefix=prefix):
        file_time=re.compile('\d{4}-\d{2}-\d{2}').findall(b.key)
        if file_time:
            get_time=time.strptime(file_time[0],'%Y-%m-%d')
            if get_time.tm_year<now_year or get_time.tm_mon<now_mon:
                print(b.key)
                bucket.delete_object(b.key)